﻿Imports System.Web.Services
Imports System.Web.Configuration

Public Class Klasifikasi
    Inherits System.Web.UI.Page

    Dim connectionString As String
    Dim klasifikasiDb As KlasifikasiDb
    Shared CurrentID As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        connectionString = WebConfigurationManager.ConnectionStrings("bisaska").ConnectionString
        klasifikasiDb = New KlasifikasiDb(connectionString)

        KlasifikasiGridView.DataBind()
        KlasifikasiTabelForm.Visible = False
        AddButton.Enabled = True
    End Sub

    Sub ResetState()
        IdKlasifikasi.Text = "0"
        AddButton.Enabled = True
        KlasifikasiTabelForm.Visible = False
        KodeKlasifikasi.Text = ""
        NilaiGuna.Text = ""
        Deskripsi.Text = ""
        NomorSE.Text = ""
        TanggalSE.Text = ""
        KlasifikasiGridView.SelectedIndex = -1
        JenisDokumen.DataBind()
        Status.SelectedValue = 0
    End Sub

    Protected Sub KlasifikasiGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles KlasifikasiGridView.RowCommand
        If e.CommandName = "CopyCommand" Then
            ResetState()
            IdKlasifikasi.Text = "0"
            KodeKlasifikasi.Text = KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(1).Text
            NilaiGuna.Text = KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(4).Text
            Deskripsi.Text = KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(2).Text
            NomorSE.Text = KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(5).Text
            TanggalSE.Text = Convert.ToDateTime(KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(6).Text)
            JenisDokumen.Text = KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(9).Text
            If KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(7).Text = "Aktif" Then
                Status.SelectedValue = 0
            ElseIf KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(7).Text = "Pasif" Then
                Status.SelectedValue = 1
            End If
            CurrentID = IdKlasifikasi.Text
            KlasifikasiTabelForm.Visible = True
        ElseIf e.CommandName = "EditCommand" Then
            ResetState()
            IdKlasifikasi.Text = KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(0).Text
            KodeKlasifikasi.Text = KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(1).Text
            NilaiGuna.Text = KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(4).Text
            Deskripsi.Text = KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(2).Text
            NomorSE.Text = KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(5).Text
            TanggalSE.Text = Convert.ToDateTime(KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(6).Text)
            JenisDokumen.Text = KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(9).Text
            If KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(7).Text = "Aktif" Then
                Status.SelectedValue = 0
            ElseIf KlasifikasiGridView.Rows(e.CommandArgument.ToString).Cells(7).Text = "Pasif" Then
                Status.SelectedValue = 1
            End If
            CurrentID = IdKlasifikasi.Text
            KlasifikasiTabelForm.Visible = True
        End If
    End Sub


    Protected Sub SaveButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveButton.Click
        Try
            If Equals("0", IdKlasifikasi.Text) Then
                klasifikasiDb.insert(KodeKlasifikasi.Text, Deskripsi.Text, JenisDokumen.SelectedItem.Value, NilaiGuna.Text, NomorSE.Text, TanggalSE.Text, Status.SelectedValue)
                KlasifikasiGridView.DataBind()
            Else
                klasifikasiDb.update(IdKlasifikasi.Text, KodeKlasifikasi.Text, Deskripsi.Text, JenisDokumen.SelectedItem.Value, NilaiGuna.Text, NomorSE.Text, TanggalSE.Text, Status.SelectedValue)
                KlasifikasiGridView.DataBind()
            End If
            ResetState()

            InfoModalPopupExtender.Show()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub AddButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles AddButton.Click
        ResetState()
        KlasifikasiTabelForm.Visible = True
        CurrentID = IdKlasifikasi.Text
    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CancelButton.Click
        ResetState()
    End Sub

    Protected Sub KlasifikasiGridView_PageIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles KlasifikasiGridView.PageIndexChanged
        ResetState()
    End Sub

    <WebMethod()>
    Public Shared Function IsExist(ByVal KodeKlasifikasi As String) As String
        Dim klasifikasiDb As New KlasifikasiDb(WebConfigurationManager.ConnectionStrings("bisaska").ConnectionString)
        If klasifikasiDb.isExist(CurrentID, KodeKlasifikasi) Then
            Return "1"
        End If

        Return "0"
    End Function

    Protected Sub MainScriptManager_AsyncPostBackError(ByVal sender As Object, ByVal e As System.Web.UI.AsyncPostBackErrorEventArgs)
        MainScriptManager.AsyncPostBackErrorMessage = e.Exception.Message
    End Sub

    Protected Sub ButtonInfo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonInfo.Click
        InfoModalPopupExtender.Hide()
    End Sub
End Class